package com.crane.oss.web.controller.sports;


import com.crane.common.core.dto.MultiResponse;
import com.crane.common.core.dto.SingleResponse;
import com.crane.oss.sports.XunFeiCommonApiService;
import com.crane.oss.sports.bean.resquest.QuerySportItemRuleDetailReq;
import com.crane.oss.web.BaseBusinessController;
import com.crane.oss.xunfei.req.QuerySportItemRuleReq;
import com.crane.oss.xunfei.req.XunFeiApiRequest;
import com.crane.oss.xunfei.resp.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author bealon
 */

@Api(value = "SportsSettingsController", tags = {"运动设置"})
@RestController
@RequestMapping("/setting")
public class SportsSettingsController extends BaseBusinessController {

    @Resource
    XunFeiCommonApiService xunFeiCommonApiService;

    @ApiOperation(value = "课程时间设置", notes = "课程时间设置")
    @PostMapping("/courseConfig")
    public SingleResponse courseConfig() {
        XunFeiApiRequest request = new XunFeiApiRequest();
        ApiSingleResponse<SportConfigCourseResp> response = xunFeiCommonApiService.getSportConfigCourse(request, getPartyId().toString(), null, getUserId().toString());
        ;
        if (null != response.getMsg() && response.getMsg().equals(ApiResponse.SUCCESS)) {
            return SingleResponse.of(response.getData());
        }
        return SingleResponse.buildFailure("请求接口失败");
    }


    @ApiOperation(value = "预警心率持续时长", notes = "预警心率持续时长")
    @PostMapping("/warningHeartRate")
    public MultiResponse warningHeartRateList() {

        XunFeiApiRequest request = new XunFeiApiRequest();
        ApiListResponse<SportConfigHeart> response = xunFeiCommonApiService.getSportConfigHeart(request, getPartyId().toString(), null, getUserId().toString());
        ;
        if (null != response.getMsg() && response.getMsg().equals(ApiResponse.SUCCESS)) {
            return MultiResponse.ofWithoutTotal(response.getData());
        }
        return MultiResponse.buildFailure("请求接口失败");
    }


    @ApiOperation(value = "项目管理-列表", notes = "项目管理-列表")
    @PostMapping("/item/list")
    public MultiResponse itemList() {
        XunFeiApiRequest request = new XunFeiApiRequest();
        ApiListResponse<SportItemListResp> response = xunFeiCommonApiService.getSportItemList(request, getPartyId().toString(), null, getUserId().toString());
        ;
        if (null != response.getMsg() && response.getMsg().equals(ApiResponse.SUCCESS)) {
            return MultiResponse.ofWithoutTotal(response.getData());
        }
        return MultiResponse.buildFailure("请求接口失败");
    }


    @ApiOperation(value = "项目管理-详情", notes = "成绩管理-删除")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QuerySportItemRuleDetailReq")
    @PostMapping("/item/rule")
    public SingleResponse itemRule(@RequestBody QuerySportItemRuleDetailReq request) {
        Map<String, Object> rusult = new HashMap<>();
        QuerySportItemRuleReq querySportItemRuleReq = new QuerySportItemRuleReq();
        querySportItemRuleReq.setItemId(request.getItemId());
        querySportItemRuleReq.setGender(request.getXsxb());
        ApiListResponse<SportItemRuleResp> response = xunFeiCommonApiService.getSportItemRule(querySportItemRuleReq, getPartyId().toString(), null, getUserId().toString());
        ;
        if (null != response.getMsg() && response.getMsg().equals(ApiResponse.SUCCESS)) {
            List<SportItemRuleResp> ruleRespList = response.getData().stream().sorted(Comparator.comparing(SportItemRuleResp::getLevel).thenComparing(SportItemRuleResp::getNjInfo)).collect(Collectors.toList());
            List<String> colName = new ArrayList<>();
            colName.add("级别");

            List<Map<String, String>> colList = new LinkedList<>();
            Map<String, String> map0 = new HashMap<>();
            Map<String, String> map1 = new HashMap<>();
            Map<String, String> map2 = new HashMap<>();
            Map<String, String> map3 = new HashMap<>();
            Set<String> mapSet=new HashSet<>();
            for (SportItemRuleResp sportItemRuleResp : ruleRespList) {
                if (!colName.contains(sportItemRuleResp.getNjInfo())) {
                    colName.add(sportItemRuleResp.getNjInfo());
                }
                if (sportItemRuleResp.getLevel().equals("优秀")) {
                    if(!mapSet.contains(sportItemRuleResp.getLevel())){
                        colList.add(map0);
                        map0.put("级别",sportItemRuleResp.getLevel());
                        mapSet.add(sportItemRuleResp.getLevel());
                    }
                    map0.put(sportItemRuleResp.getNjInfo(), sportItemRuleResp.getScore());
                } else if (sportItemRuleResp.getLevel().equals("良好")) {
                    if(!mapSet.contains(sportItemRuleResp.getLevel())){
                        colList.add(map1);
                        map1.put("级别",sportItemRuleResp.getLevel());
                        mapSet.add(sportItemRuleResp.getLevel());
                    }
                    map1.put(sportItemRuleResp.getNjInfo(), sportItemRuleResp.getScore());
                } else if (sportItemRuleResp.getLevel().equals("及格")) {
                    if(!mapSet.contains(sportItemRuleResp.getLevel())){
                        colList.add(map2);
                        map2.put("级别",sportItemRuleResp.getLevel());
                        mapSet.add(sportItemRuleResp.getLevel());
                    }
                    map2.put(sportItemRuleResp.getNjInfo(), sportItemRuleResp.getScore());
                } else if (sportItemRuleResp.getLevel().equals("不及格")) {
                    if(!mapSet.contains(sportItemRuleResp.getLevel())){
                        colList.add(map3);
                        map3.put("级别",sportItemRuleResp.getLevel());
                        mapSet.add(sportItemRuleResp.getLevel());
                    }
                    map3.put(sportItemRuleResp.getNjInfo(), sportItemRuleResp.getScore());
                }
            }

            rusult.put("col", colName);
            rusult.put("colList", colList);
            return SingleResponse.of(rusult);
        }
        return SingleResponse.buildFailure("请求接口失败");
    }

}
